package com.sxbbc.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.bank.service.IBankLoanFieldMaintainSubService;
import com.sxbbc.bank.service.IBankLoanService;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.core.dto.banks.BankLoanFieldMaintainSubVo;
import com.sxbbc.common.core.dto.banks.BankLoanVo;
import com.sxbbc.common.core.entity.banks.BankLoan;
import com.sxbbc.common.core.entity.banks.BankLoanFieldMaintainSub;
import com.sxbbc.common.core.mapper.banks.BankLoanMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-09-09
 */
@Service
public class BankLoanServiceImpl extends ServiceImpl<BankLoanMapper, BankLoan> implements IBankLoanService {

    @Autowired
    private IBankLoanFieldMaintainSubService iBankLoanFieldMaintainSubService;


    /**
     * 数据list
     * @param page
     * @param bankLoan
     * @return
     */
    public RestResponse listDate(BasePage<BankLoan> page, BankLoan bankLoan){
        QueryWrapper<BankLoan> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        super.page(page,wrapper);
        return RestFactory.success(page.getRecords(), page.getTotal());
    };


    /**
     * 保存
     * @param bankLoanVo
     * @return
     */
    public  BankLoan saveBankLoan(BankLoanVo bankLoanVo){
        String id=getUuid();
        BankLoan bankLoan=bankLoanVo.getBankLoan();
        LocalDateTime localDateTime=LocalDateTime.now();
        bankLoan.setCreateTime(localDateTime);
        bankLoan.setBankLoanId(id);
        bankLoan.setId(27);
        if(super.save(bankLoan)){
            List<BankLoanFieldMaintainSub> list=bankLoanVo.getBankLoanFieldMaintainSubs();
            for (int i = 0; i < list.size(); i++) {
                BankLoanFieldMaintainSub bankLoanFieldMaintainSub=list.get(i);
                bankLoanFieldMaintainSub.setBankLoanId(id);
                iBankLoanFieldMaintainSubService.save(bankLoanFieldMaintainSub);
            }
        }
        return bankLoan;
    }



    public BankLoanVo getBankLoanById(String id){
        //返回vo
        BankLoanVo bankLoanVo=new BankLoanVo();
        //基本信息
        QueryWrapper<BankLoan> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        BankLoan bankLoan=super.getOne(wrapper);
        bankLoanVo.setBankLoan(bankLoan);

        //属性信息
        QueryWrapper<BankLoanFieldMaintainSub> wr = new QueryWrapper<>();
        wr.eq("bank_loan_id",bankLoan.getBankLoanId());
        wr.orderByAsc("sort");
        List<BankLoanFieldMaintainSub> bankLoanFieldMaintainSubs=iBankLoanFieldMaintainSubService.list(wr);

        List<BankLoanFieldMaintainSubVo> bankLoanFieldMaintainSubVos=new ArrayList<>(bankLoanFieldMaintainSubs.size());
        for (int i = 0; i < bankLoanFieldMaintainSubs.size(); i++) {
            //返回vo对象
            BankLoanFieldMaintainSubVo bankLoanFieldMaintainSubVo=new BankLoanFieldMaintainSubVo();
            //循环获取对象
            BankLoanFieldMaintainSub bankLoanFieldMaintainSub=bankLoanFieldMaintainSubs.get(i);
            bankLoanFieldMaintainSubVo.setBankLoanId(bankLoanFieldMaintainSub.getBankLoanId());
            bankLoanFieldMaintainSubVo.setFieldName(bankLoanFieldMaintainSub.getFieldName());
            bankLoanFieldMaintainSubVo.setInputType(bankLoanFieldMaintainSub.getInputType());
            bankLoanFieldMaintainSubVo.setSort(bankLoanFieldMaintainSub.getSort());
            List<String> strings=new ArrayList<>();
            String value=bankLoanFieldMaintainSub.getInputValue();
            if(bankLoanFieldMaintainSub.getInputType()!=4){
                try {
                    String [] arr=value.split(",");
                    for (int j = 0; j < arr.length; j++) {
                        strings.add(arr[j]);
                    }
                }catch (Exception e){
                    strings=null;
                }
            }
            bankLoanFieldMaintainSubVo.setInputValueList(strings);
            bankLoanFieldMaintainSubVo.setInputValue(value);
            bankLoanFieldMaintainSubVos.add(bankLoanFieldMaintainSubVo);
        }

        bankLoanVo.setBankLoanFieldMaintainSubVos(bankLoanFieldMaintainSubVos);
        return  bankLoanVo;
    }


    /**
     * 修改
     * @param bankLoanVo
     * @return
     */
    public  int update(BankLoanVo bankLoanVo){
        BankLoan bankLoan=bankLoanVo.getBankLoan();
        LocalDateTime localDateTime=LocalDateTime.now();
        bankLoan.setCreateTime(localDateTime);
        if(super.updateById(bankLoan)){
            //把原有数据删除
            QueryWrapper<BankLoanFieldMaintainSub> wrapper =new QueryWrapper<>();
            wrapper.eq("bank_loan_id",bankLoan.getBankLoanId());
            if(iBankLoanFieldMaintainSubService.remove(wrapper)){
                //再次添加
                List<BankLoanFieldMaintainSub> list=bankLoanVo.getBankLoanFieldMaintainSubs();
                for (int i = 0; i < list.size(); i++) {
                    BankLoanFieldMaintainSub bankLoanFieldMaintainSub=list.get(i);
                    bankLoanFieldMaintainSub.setBankLoanId(bankLoan.getBankLoanId());
                    iBankLoanFieldMaintainSubService.save(bankLoanFieldMaintainSub);

                }
            }

        }
        return 0;
    }



    public Map<String,String> getDataForMap(){
        List<BankLoan> bankLoans=super.list();
        Map<String,String> map=new HashMap<>();
        for (int i = 0; i < bankLoans.size(); i++) {
            BankLoan bankLoan=bankLoans.get(i);
            map.put(bankLoan.getId().toString(),bankLoan.getLoanName());
        }
        return map;
    }

    public static String getUuid(){
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        return uuid;
    }



}
